package gui;

import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import persistencia.ReclamacionesDAO;
import pojos.Reclamacion;

public class JDialogReclamaciones extends javax.swing.JDialog {

    int proceso;
    PanelReclamaciones panel;
    ReclamacionesDAO dao = new ReclamacionesDAO();

    public JDialogReclamaciones(int proceso,PanelReclamaciones panel) {
        this.proceso = proceso;
        this.panel=panel;
        initComponents();
        switch(proceso){
                case 1:
                    fieldIdReclamacion.setVisible(false);
                    labelIdReclamacion.setVisible(false);
                    break;                
                case 2:
                    labelInstrucciones.setText("Introduzca la id de la reclamacion que desee eliminar");
                    btnGuardar.setText("Borrar");
                    fielDescripcion.setVisible(false);
                    comboCodEmpleado.setVisible(false);
                    comboCodPaciente.setVisible(false);
                    labelDescripcion.setVisible(false);
                    labelCodEmpleado.setVisible(false);
                    labelCodPaciente.setVisible(false);
                    break;
                case 3:
                    labelInstrucciones.setText("Introduzca la id de la reclamacion que desee modificar");
                    btnGuardar.setText("Modificar");
                    break;
            }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnCancelar = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();
        labelIdReclamacion = new javax.swing.JLabel();
        labelCodEmpleado = new javax.swing.JLabel();
        labelCodPaciente = new javax.swing.JLabel();
        labelDescripcion = new javax.swing.JLabel();
        fieldIdReclamacion = new javax.swing.JTextField();
        comboCodEmpleado = new javax.swing.JComboBox();
        fielDescripcion = new javax.swing.JTextField();
        comboCodPaciente = new javax.swing.JComboBox();
        labelInstrucciones = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        btnCancelar.setText("cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnGuardar.setText("guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        labelIdReclamacion.setText("idReclamacion");

        labelCodEmpleado.setText("cod empleado");

        labelCodPaciente.setText("cod paciente");

        labelDescripcion.setText("descripcion");

        comboCodEmpleado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4" }));

        comboCodPaciente.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(80, 80, 80)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelCodPaciente)
                    .addComponent(labelDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCancelar)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(btnGuardar)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(labelIdReclamacion)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(fieldIdReclamacion, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(labelCodEmpleado)
                                .addGap(92, 92, 92)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(comboCodPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(fielDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(comboCodEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addGap(0, 58, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(labelInstrucciones)
                .addGap(162, 162, 162))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(labelInstrucciones)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelIdReclamacion)
                    .addComponent(fieldIdReclamacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelCodEmpleado)
                    .addComponent(comboCodEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelCodPaciente)
                    .addComponent(comboCodPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelDescripcion)
                    .addComponent(fielDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(76, 76, 76)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnGuardar))
                .addGap(49, 49, 49))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        try {
            this.setVisible(false);
            switch (proceso) {
                case 1:agregar();break;
                case 2:eliminar();break;
                case 3:modificar();break;
            }
        } catch (ParseException ex) {
            Logger.getLogger(JDialogReclamaciones.class.getName()).log(Level.SEVERE, null, ex);
        }
        panel.Buscar();
    }//GEN-LAST:event_btnGuardarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JComboBox comboCodEmpleado;
    private javax.swing.JComboBox comboCodPaciente;
    private javax.swing.JTextField fielDescripcion;
    private javax.swing.JTextField fieldIdReclamacion;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel labelCodEmpleado;
    private javax.swing.JLabel labelCodPaciente;
    private javax.swing.JLabel labelDescripcion;
    private javax.swing.JLabel labelIdReclamacion;
    private javax.swing.JLabel labelInstrucciones;
    // End of variables declaration//GEN-END:variables
    private void agregar() throws ParseException {
        Reclamacion agregar = new Reclamacion();
        agregar.setDescripcion(fielDescripcion.getText());
        dao.guardaReclamacion(agregar);
    }

    private void eliminar() {
        int idAEliminar=Integer.parseInt(fieldIdReclamacion.getText());        
        Reclamacion personaBuscada = dao.obtenReclamacion(idAEliminar);
        dao.eliminaReclamacion(personaBuscada);
    }

    private void modificar() {
        int idAModificar=Integer.parseInt(fieldIdReclamacion.getText());        
        Reclamacion personaBuscada = dao.obtenReclamacion(idAModificar);
        personaBuscada.setDescripcion(fielDescripcion.getText());
        dao.actualizaReclamacion(personaBuscada);
    }
}
